#include "ffmpeg_audio_queue.h"

audioQueue::audioQueue()
{
    cout << "this is audioQueue init..." << endl;
    pthread_mutex_init(&queue_mutex, NULL);
    pthread_cond_init(&queue_cond, NULL);
}

audioQueue::~audioQueue()
{
    cout << "this is audioQueue destroyed..." << endl;
    pthread_mutex_destroy(&queue_mutex);
    pthread_cond_destroy(&queue_cond);
}

int audioQueue::putAudioDataToQueue(audio_data_packet *audio_packet)
{
    pthread_mutex_lock(&queue_mutex);
    audio_queue.push(audio_packet);
    pthread_cond_broadcast(&queue_cond);
    pthread_mutex_unlock(&queue_mutex);
    return 0;
}

audio_data_packet *audioQueue::getAudioDataFromQueue()
{
    pthread_mutex_lock(&queue_mutex);
    while (audio_queue.empty())
    {
        pthread_cond_wait(&queue_cond, &queue_mutex);
    }
    audio_data_packet *tmp_packet = audio_queue.front(); // 此处队列里面的Audio数据是的new出来的！
    audio_queue.pop();
    pthread_mutex_unlock(&queue_mutex);
    return tmp_packet;
}

int audioQueue::getAudioQueueSize()
{
    int size = -1;
    pthread_mutex_lock(&queue_mutex);
    size = audio_queue.size();
    pthread_mutex_unlock(&queue_mutex);
    return size;
}